package com.zt.liulishuoyingyudanci.a.b.c.d.e.f.g.wxapi

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.wx.login.WXBaseRespEntity
import com.wx.login.WXLoginBroadcast
import com.wx.login.WXUtils

class WXEntryActivity : AppCompatActivity(), IWXAPIEventHandler {
    companion object {
        const val TAG = "WXEntryActivity"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //第三方开发者如果使用透明界面来实现WXEntryActivity，需要判断handleIntent的返回值，如果返回值为false，则说明入参不合法未被SDK处理，应finish当前透明界面，避免外部通过传递非法参数的Intent导致停留在透明界面，引起用户的疑惑
        try {
            val result = WXUtils.iwxapi.handleIntent(intent, this)
            if (!result) {
                Log.e(TAG, "参数不合法，未被SDK处理，退出")
                finish()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        WXUtils.iwxapi.handleIntent(data, this)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        setIntent(intent)
        WXUtils.iwxapi.handleIntent(intent, this)
        finish()
    }

    override fun onReq(baseReq: BaseReq) {
        Log.e(TAG, "baseReq:$baseReq")
    }

    override fun onResp(baseResp: BaseResp) {
        if (baseResp.type == ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX) {
            Log.e(TAG, "微信分享操作..... : " + baseResp.errCode)
            finish()
            when (baseResp.errCode) {
                BaseResp.ErrCode.ERR_OK -> Toast.makeText(this, "分享成功", Toast.LENGTH_SHORT).show()
                BaseResp.ErrCode.ERR_USER_CANCEL -> Toast.makeText(
                    this,
                    "分享被取消",
                    Toast.LENGTH_SHORT
                ).show()
                BaseResp.ErrCode.ERR_AUTH_DENIED -> Toast.makeText(
                    this,
                    "分享被拒绝",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
        if (baseResp.type == ConstantsAPI.COMMAND_SENDAUTH) {
            Log.e(TAG, "微信登录操作..... : " + baseResp.errCode)
            val (code) = Gson().fromJson(Gson().toJson(baseResp), WXBaseRespEntity::class.java)
            Log.e(TAG, "code: $code")
            val intent = Intent()
            when (baseResp.errCode) {
                BaseResp.ErrCode.ERR_OK -> {
                    intent.action = packageName + WXLoginBroadcast.BROADCAST_ACTION_SUCCESS
                    Log.e(TAG, "action..... : " + intent.action)
                    intent.putExtra(WXLoginBroadcast.KEY_WX_CODE, code)
                }
                else -> {
                    intent.action = packageName + WXLoginBroadcast.BROADCAST_ACTION_FAILED
                }
            }
            sendBroadcast(intent)
            finish()
        }
    }
}